/**
 * 系统菜单面板。
 * 由于此面板只使用一次，不需要使用继承。
 * @author linzongxue
 * @create_date 2012-1-8
 */
define(function(){
	//var expandedMenuIds;//重新加载菜单后要自动展开的菜单
	var menuClick = function(node){
		var target = node.attributes.value;
		if (target){
			if (target.match(/^js\s*:/)){
				var js = target.replace(/^js\s*:/, '').trim();
				(new Function(js))();
			}
			else{
				var tips = node.getPath('text').replace('//', '');
				au.openTab(node.id, target, node.text, tips);
			}
		}
		else if (!node.isLeaf()){
			node.toggle();
		}
	};
	var createItem = function(node){
		var treePanel = new Ext.tree.TreePanel({
			title: node.text,
			useArrows: true,
			border: false,
			bodyStyle: 'padding-top:4px;',
			rootVisible: false,
	        root: {
	        	expanded: true,
	        	children: node.children
	        },
	        listeners: {
	        	click: menuClick
	        }
		});
		return treePanel;
	};
	var loadMenus = function(menuPanel){
		menuPanel.removeAll();
		cu.get({
			url: 'sys/menu/getUserMenus.do',
			success: function(data){
				//这里的data是树形结构的数据（没有根节点）
				Ext.each(data, function(node){
					menuPanel.add(createItem(node));
				});
				menuPanel.doLayout();
			}
		});
	};
	var menuPanel = new Ext.Panel({
        region: 'west',
        layout: 'accordion',
        title: '系统菜单',
        width: 180,
        margins:'0 0 3 3',
        collapsible: true,
        collapseMode: 'mini',
        autoScroll: true,
        split: true,
		items:[],
		tools:[
			//{id: 'refresh', handler: function(event, toolEl, panel, tc){panel.refresh();}}
		],
		listeners: {
			afterrender: function(){
				this.refresh();
			}
		},
		//向外提供刷新方法
		refresh: function(){
			loadMenus(this);
		}
	});
	
	return menuPanel;
});